<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class CouponExtract extends Model
{
    protected $dateFormat = 'U';
    //
    protected $table = "lexiang_coupon_extract";

    public function add($data)
    {
    	$amount = CouponShop::where('id',$data['shop_id'])->value('amount');
    	if($amount < $data['amount']){
    		return [
    			'success'=>false,
    			'code'=>'004',
    			'message'=>'余额不足，无法申请'
    		];
    	}
    	$this->coupon_shop_id = $data['shop_id'];
    	$this->amount = $data['amount'];
    	$this->bank_number = $data['bank_number'];
    	$this->bank_open = $data['bank_open'];
    	$this->bank_name = $data['bank_name'];
    	$this->bank_tel = $data['bank_tel'];
    	$rmo = ($amount - $data['amount']);
    	CouponShop::where('id',$data['shop_id'])->update(['amount'=>$rmo]);
    	if($this->save()){
    		return [
    			'success'=>true,
    			'code'=>'000',
    			'message'=>'申请已经提交！',
    			'amount'=>$rmo
    		];
    	}else{
    		return [
    			'success'=>false,
    			'code'=>'006',
    			'message'=>'记录出错，请稍后再试！'
    		];
    	}
    }
}
